package sk.naive.talker.props;

import java.util.*;

/**
 * Base class for properties with set of values.
 *
 * @author <a href="mailto:richter@bgs.sk">Richard Richter</a>
 * @version $Revision: 1.8 $ $Date: 2004/11/22 20:34:25 $
 */
public class MultiValueProperty extends EnumeratedProperty {
	public final boolean checkValue(Object o) {
		for (Object item : (Iterable) o) {
			if (!super.checkValue(itemToString(item))) {
				return false;
			}
		}
		return true;
	}

	public String toString(Object o) {
		StringBuilder sb = new StringBuilder();
		for (Object item : (Iterable) o) {
			if (sb.length() > 0) {
				sb.append('\n');
			}
			sb.append(itemToString(item));
		}
		return sb.toString();
	}

	protected String itemToString(Object item) {
		if (item == null) {
			return null;
		}
		return item.toString();
	}

	public Object toObject(String s) {
		Collection collection = createCollection();
		if (s != null && s.length() > 0) {
			for (String item : s.split("\\n")) {
				collection.add(itemToObject(item));
			}
		}
		return collection;
	}

	protected Object itemToObject(String item) {
		return item;
	}

	protected Collection createCollection() {
		return new HashSet();
	}

	public Object defaultValue() {
		return createCollection();
	}
}
